package gamecomm.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.*;
 
import gamecomm.library.DatabaseHandler;
import gamecomm.library.UserFunctions;
 
public class DashboardActivity extends Activity {
    UserFunctions userFunctions;
    Button btnLogout;
    Button btngames;
    Button btnaddgame;
    Button btnconnections;
    Button btnsettings;
    Button btnSearch;
    EditText searchBar;
    
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        /**
         * Dashboard Screen for the application
         * */
        // Check login status in database
        userFunctions = new UserFunctions();
        if(userFunctions.isUserLoggedIn(getApplicationContext())){
            setContentView(R.layout.dashboard);
            btngames = (Button) findViewById(R.id.games);
            btnaddgame = (Button) findViewById(R.id.addmygame);
            btnconnections = (Button) findViewById(R.id.connections);
            btnsettings = (Button) findViewById(R.id.settings);
            searchBar = (EditText) findViewById(R.id.searchBar);
            btnSearch = (Button) findViewById(R.id.searchbutton);
           
            
            btnSearch.setOnClickListener(new View.OnClickListener() {
 
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                	//WHEN SEARCH BUTTON IS HIT DO SOMETHING
                	String name = searchBar.getText().toString();
        			UserFunctions userfunc=new UserFunctions();
        			//JSONObject json=userfunc.checkForUser(name);
        			//DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        			//try {
                      //  if (json.getString(KEY_SUCCESS) != null) {
                         //   String res = json.getString(KEY_SUCCESS);
                           // if(Integer.parseInt(res) == 1){
                          
                              //  DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                                //JSONObject json_user = json.getJSONObject("user");
        			
                   
                     Intent blank = new Intent(getApplicationContext(), blank.class);
                     // Clear all previous data in database
                    // userfunc.logoutUser(getApplicationContext());
                   //  db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        
                 
                   //  db.getUserDetails();
                     startActivity(blank);
                     finish();
        			//DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
                //}
                       // }
                        //} catch (JSONException e) {
                            //e.printStackTrace();
                       // }
                }
            });
            
            /*
            btnaddgame.setOnClickListener(new View.OnClickListener() {
            	 
                public void onClick(View view) {
        			UserFunctions userfunc=new UserFunctions();
        			
                     Intent adding = new Intent(getApplicationContext(), AddGameActivity.class);
                     startActivity(adding);
                     finish();

                }
            });
            */
            
            btnLogout = (Button) findViewById(R.id.btnLogout);
 
            btnLogout.setOnClickListener(new View.OnClickListener() {
 
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    userFunctions.logoutUser(getApplicationContext());
                    Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(login);
                    // Closing dashboard screen
                   // finish();
                }
            });
            
            
            btnsettings.setOnClickListener(new View.OnClickListener() {
 
                public void onClick(View view) {
                	    userFunctions.logoutUser(getApplicationContext());
                        Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
                        startActivity(i);
                        // Close Registration View
                       // finish();
                }
            });
            
            btngames.setOnClickListener(new View.OnClickListener() {        	 
                public void onClick(View view) {
                	userFunctions.logoutUser(getApplicationContext());
                        Intent i = new Intent(getApplicationContext(),MyGameListActivity.class);
                        startActivity(i);
                        // Close Registration View
                        //finish();
                }
            });
            
 
        }else{
            // user is not logged in show login screen
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            // Closing dashboard screen
            //finish();
        }
    }
}

